package com.ziyia.servicetest;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.Button;

import androidx.core.app.NotificationCompat;

public class MyService extends Service {

    private static final String T = MyService.class.getName();
    private DownLoadBinder downLoadBinder = new DownLoadBinder();

    public static class DownLoadBinder extends Binder {
        public void startDownLoad() {
            Log.d(T, "开始下载.......");
        }

        public int getProgress() {
            Log.d(T, "获取进度.......");
            return 0;
        }
    }

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return downLoadBinder;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(T, "onCreate executed");
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("MyService", "前台Service通知", NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
        }
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification builder = new NotificationCompat.Builder(this, "MyService")
                .setContentTitle("标题在此")
                .setContentText("通知内容")
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, builder);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Thread onStartCommand_executed = new Thread(() -> {
                Log.d(T, "onStartCommand executed");

                stopSelf();
        });
        onStartCommand_executed.start();
        Log.d(T, "onStartCommand executed");
        return 1;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(T, "onDestroy executed");
    }
}